package com.weavernorth.dsjx;

import com.weavernorth.humanresource.SalarRangeUtil;
import com.weavernorth.sysap.util.CommonToSapUtil;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.dateUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import java.util.Iterator;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.dsjx
 * @date: 2020/9/1 16:32
 * @description: 【品牌】商品/物流 季度奖金流程:电商绩效推送SAP接口
 */
public class dsjxToSAP implements Action {
    private Logger log = Logger4jUtil.getLoggerByName("/action/dsjxToSAP/");
    @Override
    public String execute(RequestInfo requestInfo) {
        BaseBean bb=new BaseBean();
        //推送
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        log.info("电商绩效传SAP数据开始。。。");
        RecordSet rs=new RecordSet();
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid=" + strRequestid);
        String workflowid = requestInfo.getWorkflowid();//流程ID
        log.info("workflowid:"+workflowid);
        //【品牌】零售经理奖金申请（月均店效、正价占比、新品售出）:workflowid=87521
        //【品牌】商品季度绩效奖金流程;workflowid=39021;
        //【品牌】物流月度绩效奖金流程:workflowid=46021;
        String strTable = requestInfo.getRequestManager().getBillTableName();

        log.info("表名：" + strTable);
        String strSql="";
        if(!workflowid.equals("87521")&&!workflowid.equals("1051021")){
            strSql="select * from "+strTable+" a,"+strTable+"_dt1 b where a.id=b.mainid and a.requestid="+strRequestid+"";
        }else{
            strSql="SELECT a.REQUESTID, a.czr, a.yf, a.nd, v.ygh, (NVL(b.jjhj,0)+NVL(b2.jjhj,0)+NVL(b3.jjhj,0)) jjhj FROM "+strTable+" a INNER JOIN v_tableCenter_view v ON a.id=v.mainid " +
                    "left JOIN v_table1_view b ON a.id = b.mainid AND v.ygh = b.ygh " +
                    "left JOIN v_table2_view b2 ON a.id = b2.mainid AND v.ygh = b2.ygh " +
                    "left JOIN v_table3_view b3 ON a.id = b3.mainid AND v.ygh = b3.ygh " +
                    "where a.requestid="+strRequestid+"";
        }

        log.info("执行查询语句："+strSql);
        rs.executeQuery(strSql);
        //报文请求体
        StringBuffer requestXml;
        requestXml = new StringBuffer();
        //拼接请求体
        requestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>" +
                "<soapenv:Header/>" + "<soapenv:Body>" + "<urn:ZHRI_078>" + " <RETURN>" +
                "<!--Zero or more repetitions:-->" +
                "<item>" +
                "<TYPE></TYPE>" +
                "<ID></ID>" +
                "<NUMBER></NUMBER>" +
                "<MESSAGE></MESSAGE>" +
                "<LOG_NO></LOG_NO>" +
                "<LOG_MSG_NO></LOG_MSG_NO>" +
                "<MESSAGE_V1></MESSAGE_V1>" +
                "<MESSAGE_V2></MESSAGE_V2>" +
                "<MESSAGE_V3></MESSAGE_V3>" +
                "<MESSAGE_V4></MESSAGE_V4>" +
                "<PARAMETER></PARAMETER>" +
                "<ROW></ROW>" +
                "<FIELD></FIELD>" +
                "<SYSTEM></SYSTEM>" +
                "</item>" +
                "</RETURN>" +
                "<T_9136>" +
                "<!--Zero or more repetitions:-->");
        //workflowid正式：87521;workflowid仿真：1051021;
        if(workflowid.equals("87521")||workflowid.equals("1051021")){
            try {

                while (rs.next()) {
                    //获取员工编号
                    String strYgbh = Util.null2String(rs.getString("ygh"));
                    log.info("员工编号：" + strYgbh);
                    //奖金
                    String strjxjj = Util.null2String(rs.getString("jjhj"));
                    log.info("奖金:" + strjxjj);
                    int strnf = Integer.parseInt(Util.null2String(rs.getString("nd")));//年份
                    log.info("年份：" + strnf);
                    int stryf = Integer.parseInt(Util.null2String(rs.getString("yf")));//月份
                    log.info("月份：" + stryf);
                    //评分日期
                    String strPfrq = dateUtils.LastDayOfMonth(strnf, stryf);
                        //拼接XML
                        requestXml.append(" <item>" + "<PERNR>" + strYgbh + "</PERNR>");
                        requestXml.append(" <ZJXPF>0</ZJXPF>");
                        requestXml.append(" <ZJX_DATE>" + strPfrq + "</ZJX_DATE>");
                        requestXml.append(" <ZJE>" + strjxjj + "</ZJE>");
                        requestXml.append(" <ZTYPE>50</ZTYPE></item>");
                }
            }catch (Exception ex){
                ex.printStackTrace();
                requestInfo.getRequestManager().setMessagecontent("程序异常："+ex.toString());
                return FAILURE_AND_CONTINUE;
            }
        }else{
            while (rs.next()){
                //获取员工编号
                String strYgbh = Util.null2String(rs.getString("ygh"));
                log.info("员工编号：" + strYgbh);
                //绩效得分
                String strjxdf = Util.null2String(rs.getString("jxdf"));
                log.info("绩效得分："+strjxdf);
                //绩效奖金
                String strjxjj = Util.null2String(rs.getString("jxjj"));
                log.info("绩效奖金:"+strjxjj);
                int strnf = Integer.parseInt(Util.null2String(rs.getString("nf")));//年份
                log.info("年份："+strnf);
                //评分日期
                String strPfrq="";
                Integer zType=0;
                if (workflowid.equals("39021")){//商品季度
                    int strjd = Integer.parseInt(Util.null2String(rs.getString("jd")));//季度
                    log.info("季度："+strjd);
                    strPfrq=dateUtils.LastDayOfMonth(strnf,dateUtils.getMonthByjd(strjd));
                    zType=40;
                }else if (workflowid.equals("46021")){//
                    int stryf = Integer.parseInt(Util.null2String(rs.getString("yf")));//月份
                    log.info("月份："+stryf);
                    strPfrq=dateUtils.LastDayOfMonth(strnf,stryf);
                    zType=30;
                }else if (workflowid.equals("73521")){//【品牌】板房工艺室计件提成流程
                    int stryf = Integer.parseInt(Util.null2String(rs.getString("yf")));//月份
                    log.info("月份："+stryf);
                    strPfrq=dateUtils.LastDayOfMonth(strnf,stryf);
                    zType=50;
                }
                log.info("评分日期："+strPfrq);
                //拼接XML
                requestXml.append(" <item>" + "<PERNR>" + strYgbh + "</PERNR>");
                requestXml.append(" <ZJXPF>" + strjxdf + "</ZJXPF>");
                requestXml.append(" <ZJX_DATE>" + strPfrq + "</ZJX_DATE>");
                requestXml.append(" <ZJE>" + strjxjj + "</ZJE>");
                requestXml.append(" <ZTYPE>"+zType+"</ZTYPE></item>");
            }
        }

        requestXml.append("</T_9136>" +
                "</urn:ZHRI_078>" +
                "</soapenv:Body>" +
                "</soapenv:Envelope>");
        log.info("推送的xml:" + requestXml.toString());
        //获取电商激励地址
        String strDsjlUrl = Util.null2String(bb.getPropValue("dsjxToSap", "dsjxToSapUrl"));
        //获取用户名
        String strUsername = Util.null2String(bb.getPropValue("dsjxToSap", "username"));
        log.info("用户名:"+strUsername);
        //获取密码
        String strPassword = Util.null2String(bb.getPropValue("dsjxToSap", "password"));
        log.info("密码:"+strPassword);

        try {
//            String responseXml = CommonToSapUtil.sendPost(strDsjlUrl, requestXml.toString(), "utf-8",strUsername,strPassword);
            String responseXml = CommonToSapUtil.sendPost(strDsjlUrl, requestXml.toString(), "utf-8",strUsername,strPassword);
            log.info("返回的xml:" + responseXml);
            //返回标识
            String strReturnStatus = "";
            //返回信息
            String strMessage = "";
            Document doc = null;//解析返回值开始
            doc = DocumentHelper.parseText(responseXml);
            Iterator<Element> its2 = doc.selectNodes("//Type").iterator();
            while (its2.hasNext()) {
                Element element = its2.next();
                strReturnStatus = element.getText();
                if (!"".equals(strReturnStatus)) {
                    break;
                }
            }
            log.info("返回值:" + strReturnStatus);
            //返回值失败时,则提示失败
            if ("E".equals(strReturnStatus)) {
                Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                while (its3.hasNext()) {
                    Element element2 = its3.next();
                    strMessage = element2.getText();
                    if (!"".equals(strMessage)) {
                        break;
                    }
                }

            }
            if ("E".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;
            }


        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("程序异常："+e.toString());
            return FAILURE_AND_CONTINUE;
        }
        log.info("电商绩效传SAP数据结束。。。");
        return SUCCESS;
    }
}
